<template>
  <div id="app">
    <transition :name="transitionName">
      <router-view />
    </transition>
  </div>
</template>
<script>
export default {
  data() {
    return {
      transitionName: "slide-right",
    };
  },
  created() {
    console.log('初始加载');
    if (this.$store.getters.getIsLogin) {
      // 已登录 刷新之后 获取用户信息
      this.$store.dispatch('getInfo')
      this.$store.dispatch('getCartData')
    }
  },
  watch: {
    $route: function (newValue, oldValue) {

      if (newValue.meta.index < oldValue.meta.index) {
        this.transitionName = "slide-right";
      } else if (newValue.meta.index > oldValue.meta.index) {
        this.transitionName = "slide-left";
      } else {
        this.transitionName = "";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
#app {
  width: 100%;
  max-width: 800px;
  height: 100%;
  margin: 0 auto;
  background: #fff;
  -webkit-overflow-scrolling: touch;
  position: absolute;
  left: 0;
  top: 0;
}

.router-view {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  margin: 0 auto;
  -webkit-overflow-scrolling: touch;
}

/*切换动画*/
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  height: 100%;
  will-change: transform;
  transition: all 800ms;
  position: absolute;
  backface-visibility: hidden;
}

.slide-right-enter {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

.slide-right-leave-active {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-enter {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-leave-active {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}




// // 从右到左的 入场动画
// .fade-rigth-enter,
// .fade-left-leave-to { // fade-left-leave-to  从左到右的离场
//   opacity: 0;
//   transform: translateX(100%)
// }

// // 需要给谁加过度动画
// .fade-rigth-enter-active,
// .fade-left-enter-active,
// .fade-left-leave-to,
// .fade-rigth-leave-to{ // 一定要设置 入场 离场的 动画
//   transition: all 0.6s ease;
// }

// // 从左边移动到右边
// .fade-left-enter,
// .fade-rigth-leave-to { // fade-rigth-leave-to 从右到左动画的离场动画
//   opacity: 0;
//   transform: translateX(-100%)
// }


// // 解决过度动画中间 留白的问题
// .router-view {
//   width: 100%;
//   height: 100%;
//   position: absolute;
//   top: 0;
//   bottom: 0;
//   margin: 0 auto;
//   // 滚动的 兼容样式
//   -webkit-overflow-scrolling: touch;
// }



</style>
